package com.qinglei.recoup.system.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qinglei.recoup.common.annotation.Log;
import com.qinglei.recoup.common.domain.CommonRes;
import com.qinglei.recoup.common.exception.RedisConnectException;
import com.qinglei.recoup.common.tenant.TenantContextHolder;
import com.qinglei.recoup.common.utils.JsonUtils;
import com.qinglei.recoup.system.domain.SystemConfig;
import com.qinglei.recoup.system.pojo.*;
import com.qinglei.recoup.system.service.NurseBedService;
import com.qinglei.recoup.system.service.SystemConfigService;
import com.qinglei.recoup.system.service.TourLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;


@Api(tags = "护工相关控制器-石-已完成")
@Slf4j
@Validated
@RestController
@RequestMapping("nurse")
public class NurseController {

    @Autowired
    private SystemConfigService systemConfigService;

    @Resource
    private NurseBedService nurseBedService;

    @Resource
    private TourLogService tourLogService;

    @ApiOperation(value = "关联护工和床位-已完成")
    @Log("关联护工和床位")
    @PostMapping("/relateBeds")
    public CommonRes<Object> relateBeds(@RequestBody @Valid NurseRelateBedParam param) {
        return nurseBedService.relateBeds(param);
    }

    @ApiOperation(value = "护工分页列表-已完成")
    @GetMapping
    public CommonRes<PageVO<NurseDetailVO>> getPageList(UserSearchParam pageParam) {
        PageVO<NurseDetailVO> pageList = nurseBedService.getPageList(pageParam);
        return CommonRes.ok(pageList);
    }

    @ApiOperation(value = "添加在线巡查时间")
    @PostMapping("/MonitorTime")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "body", dataType = "String",required = true),
            @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "body", dataType = "String",required = true)
    })
    public CommonRes addTourTime(@RequestBody TimePeriodParam monitorTimeQO) throws RedisConnectException {
        //查询要录入的时间段是否与已存在的时间段有重叠时间段
        boolean flag = systemConfigService.overlap(monitorTimeQO,SystemConfigService.TYPE_TOUR_TIME);
        if (flag){
            return CommonRes.failed("录入时间与已存在的巡视时间有重叠段");
        }
        Integer tenantId = TenantContextHolder.getTenantId();
        String json = JsonUtils.objectToJson(monitorTimeQO);
        SystemConfig systemConfig = new SystemConfig();
        systemConfig.setValue(json);
        systemConfig.setType(SystemConfigService.TYPE_TOUR_TIME);
        int count = systemConfigService.getBaseMapper().insert(systemConfig);
        if (count>0){
            TourTimeVO tourTimeVO = new TourTimeVO();
            tourTimeVO.setId(systemConfig.getId());
            tourTimeVO.setStartTime(monitorTimeQO.getStartTime());
            tourTimeVO.setEndTime(monitorTimeQO.getEndTime());
            tourTimeVO.setTenantId(tenantId);
            tourLogService.addTourTimeCache(tourTimeVO);
            tourLogService.pushTourEvent(tourTimeVO, TourNotifyVO.STATUS_START);
            return CommonRes.ok("添加在线巡查时间成功");
        }
        return CommonRes.failed("添加在线巡查时间失败");
    }

    @ApiOperation(value = "查询在线巡查时间")
    @GetMapping("/MonitorTime")
    public CommonRes<List<MonitorTimeVO>> getTourTime() {
        List<MonitorTimeVO> monitorTimeVOS = new ArrayList<>();
        List<SystemConfig> systemConfigs = systemConfigService.getBaseMapper().selectList(Wrappers.<SystemConfig>lambdaQuery().eq(SystemConfig::getType, SystemConfigService.TYPE_TOUR_TIME));
        for (SystemConfig sc:systemConfigs) {
            MonitorTimeVO monitorTimeVO = JsonUtils.jsonToBean(sc.getValue(), MonitorTimeVO.class);
            monitorTimeVO.setId(sc.getId());
            monitorTimeVOS.add(monitorTimeVO);
        }
        return CommonRes.ok(monitorTimeVOS);
    }

    @ApiOperation(value = "删除在线巡查时间")
    @DeleteMapping("/MonitorTime/{id}")
    public CommonRes delTourTime(@PathVariable Integer id) throws RedisConnectException {
        SystemConfig monitorTime = systemConfigService.getById(id);
        if (monitorTime == null) {
            return CommonRes.failed("记录不存在");
        }
        TimePeriodParam monitorTimeQO = JsonUtils.jsonToBean(monitorTime.getValue(), TimePeriodParam.class);
        if (monitorTimeQO == null) {
            return CommonRes.failed("数据不合法");
        }
        int count = systemConfigService.getBaseMapper().deleteById(id);
        if (count >0) {
            TourTimeVO tourTimeVO = new TourTimeVO();
            tourTimeVO.setId(id);
            tourTimeVO.setStartTime(monitorTimeQO.getStartTime());
            tourTimeVO.setEndTime(monitorTimeQO.getEndTime());
            tourTimeVO.setTenantId(monitorTime.getTenantId());
            tourLogService.delTourTimeCache(tourTimeVO);
            tourLogService.pushTourEvent(tourTimeVO, TourNotifyVO.STATUS_END);
            return CommonRes.ok();
        }
        return CommonRes.failed("删除在线巡查时间失败");
    }

    @ApiOperation(value = "修改护工异常处理时间阈值")
    @PutMapping("/abnormalTime")
    public CommonRes uptNurseAbnormalTime(@RequestBody AbnormalTimeVO abnormalTimeVO) throws RedisConnectException {
        return systemConfigService.uptNurseAbnormalTime(abnormalTimeVO);
    }

    @ApiOperation(value = "查询护工异常处理时间阈值")
    @GetMapping("/abnormalTime")
    public CommonRes getNurseAbnormalTime() {
        return systemConfigService.getNurseAbnormalTime();
    }

}
